package my.ftpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class ServerInfo {
	private static Logger logger = Logger.getLogger(ServerInfo.class);
	private String ip;
	private String username;
	private String password;
	private String path;
	private int port = 21;

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	@Override
	public String toString() {
		return "ServerInfo [ip=" + ip + ", username=" + username
				+ ", password=" + password + ", path=" + path + ", port="
				+ port + "]";
	}

	public static List<ServerInfo> load() {
		List<ServerInfo> serverInfos = new ArrayList<ServerInfo>();

		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(
					ServerInfo.class.getResourceAsStream("/servers.conf")));
			String s;
			while ((s = br.readLine()) != null) {
				String[] ss = s.split("\t");
				if (ss.length > 3) {
					ServerInfo serverInfo = new ServerInfo();
					serverInfo.setIp(ss[0]);
					serverInfo.setUsername(ss[1].trim());
					serverInfo.setPassword(ss[2].trim());
					serverInfo.setPath(ss[3].trim());
					serverInfos.add(serverInfo);

					if (ss.length > 4) {
						serverInfo.setPort(Integer.parseInt(ss[4]));
					}
				}

			}
		} catch (Exception e) {
			logger.error(e);
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return serverInfos;
	}

	public static void main(String[] args) {
		for (ServerInfo s : ServerInfo.load()) {
			System.out.println(s);
		}
	}

}
